/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.emergency.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.emergency.domain.EmergencyInstitutions;
import com.koron.emergency.domain.EmergencyInstitutionsStaff;
import com.koron.emergency.service.api.EmergencyInstitutionsService;
import com.koron.emergency.service.api.EmergencyInstitutionsStaffService;
import com.koron.emergency.util.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

@Api(tags = "emergencyInstitutions",description = "应急机构相关接口类")
@RestController
@RequestMapping("/emergencyInstitutions")
public class EmergencyInstitutionsController {

    @Autowired
    private EmergencyInstitutionsService institutionsService;

    @Autowired
    private EmergencyInstitutionsStaffService staffService;

    @ApiOperation("保存应急机构接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id,新增不传，修改必传",  dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "水司code",  dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "name", value = "机构名称",  dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "ranking", value = "排序值",  dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pid", value = "父级节点id，没有根结点时不传",  dataType = "String",paramType = "query")
    })
    @RequestMapping(value = "/saveEmergency.htm",method = RequestMethod.POST)
    @Log(title = "保存应急机构", businessModule = "应急机构", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<Integer> saveEmergency(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore EmergencyInstitutions bean){
        if (null==user){
            MessageBean<Integer> messageBean=new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LONGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return messageBean;
        }
        bean.setUpdateUser(user.getCode());
        bean.setUpdateUserName(user.getName());
        bean.setUpdateTime(new Date());
        return institutionsService.save(user,bean);
    }

    @ApiOperation("删除应急机构接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "应急机构id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/deleteEmergency.htm",method = RequestMethod.POST)
    @Log(title = "删除应急机构", businessModule = "应急机构", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<Integer> deleteEmergency(@RequestParam final String id){

        return institutionsService.delete(id);
    }

    @ApiOperation("查询单个应急机构详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "应急机构id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/detailsEmergency.htm",method = RequestMethod.POST)
    public MessageBean<EmergencyInstitutions> detailsEmergency(@RequestParam final String id){

        return institutionsService.queryOne(id);
    }

    @ApiOperation("应急机构列表接口-根据父部门id查询子部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode",value = "水司code",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "pid",value = "父节点code,根节点的父节点id为-1",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/childListEmergency.htm",method = RequestMethod.POST)
    public MessageBean<List<EmergencyInstitutions>> childListEmergency(@RequestParam final String corpCode, @RequestParam String pid){
        return institutionsService.childList(corpCode, pid);
    }

    @ApiOperation("保存应急人员接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id",  dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "institutionsId", value = "所在机构id", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "role", value = "机构角色", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "staffCode", value = "人员code", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "staffName", value = "人员名称", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "单位code", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "departmentName", value = "单位名称", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "联系电话",  dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "dutyDescription", value = "职责描述", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "staffIndex", value = "排序",  dataType = "Integer",paramType = "query")
    })
    @RequestMapping(value = "/saveStaff.htm",method = RequestMethod.POST)
    @Log(title = "保存应急机构人员", businessModule = "应急机构", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<Integer> saveStaff(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore EmergencyInstitutionsStaff bean){
        if (null==user){
            MessageBean<Integer> messageBean=new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LONGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return messageBean;
        }
        bean.setUpdateUser(user.getCode());
        bean.setUpdateUserName(user.getName());
        bean.setUpdateTime(new Date());
        return staffService.save(user,bean);
    }

    @ApiOperation("删除人员接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "人员id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/deleteStaff.htm",method = RequestMethod.POST)
    @Log(title = "删除应急机构人员", businessModule = "应急机构", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<Integer> deleteStaff(@RequestParam final String id){

        return staffService.delete(id);
    }

    @ApiOperation("查询单个人员详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "人员id",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/detailsStaff.htm",method = RequestMethod.POST)
    public MessageBean<EmergencyInstitutionsStaff> detailsStaff(@RequestParam final String id){

        return staffService.queryOne(id);
    }

    @ApiOperation("查询某个机构下人员列表，不包含子部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "institutionsId",value = "机构id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "staffName",value = "人员名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,dataType = "int",paramType = "query"),
    })
    @RequestMapping(value = "/listStaff.htm",method = RequestMethod.POST)
    public MessageBean<DataBean<EmergencyInstitutionsStaff>> listStaff(@RequestParam final String institutionsId, @RequestParam(required = false) final String staffName,
                                                                  @RequestParam Integer page, @RequestParam Integer pageSize){
        Integer start=null;
        if (page==null||page<=0){
            page=1;
            if (pageSize==null||pageSize<=0){
                pageSize=15;
            }
        }
        start=(page<=1)?0:(page-1)*pageSize;
        MessageBean<DataBean<EmergencyInstitutionsStaff>>msg= staffService.list(institutionsId, staffName,start,pageSize);
        if (null!=msg.getData())
            msg.getData().setPageNo(page);
        return msg;
    }

    @ApiOperation("应急机构图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode",value = "水司code",required = true,dataType = "String",paramType = "query"),
    })
    @RequestMapping(value = "/listPicture.htm",method = RequestMethod.POST)
    public MessageBean<EmergencyInstitutions> listPicture(@RequestParam final String corpCode){
        return institutionsService.queryCorpAll(corpCode);
    }
}
